﻿using UnityEngine;

public class Damping : MonoBehaviour
{
    [Tooltip("阻尼系数，0~1，越接近1减速越慢")]
    public float damping = 0.9f; // 0.8 = 每秒速度减少到原来的 80%

    private Rigidbody2D rb;

    public void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        if (rb == null)
        {
            Debug.LogError("Damping requires a Rigidbody2D on " + gameObject.name);
            enabled = false;
        }
    }

    private void FixedUpdate()
    {
        if (rb == null) return;

        // 如果速度很小，直接清零
        if (rb.velocity.sqrMagnitude < 0.001f)
        {
            rb.velocity = Vector2.zero;
        }
        else
        {
            // 直接衰减速度，不加任何力
            rb.velocity *= damping;
        }
    }
}